package com.zlj.accountant.app.weiget;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.zlj.accountant.R;

public  class JDialog{
	
	AlertDialog ad;
	
	View root;
	
	Button btnOk,btnCancel;
	
	OnClickListener mOnOkClickListener;
	
	public JDialog(Context context,String title){
		Builder build = new Builder(context);
		ad = build.create();
		LayoutInflater factory = LayoutInflater.from(context);
		root = (View)factory.inflate(R.layout.lyt_page_createdialog, null);
		ad.setView(root);
		ad.show();						
		ad.getWindow().setContentView(R.layout.lyt_page_createdialog);
		root = ad.getWindow().findViewById(R.id.v);
		btnOk = (Button)ad.getWindow().findViewById(R.id.btn_ok);
		btnCancel = (Button)ad.getWindow().findViewById(R.id.btn_cancel);
		TextView tvTitle = (TextView) ad.getWindow().findViewById(R.id.txt_title);
		tvTitle.setText(title);
		regListener();
    }
	
	private void regListener(){
	    btnCancel.setOnClickListener(new android.view.View.OnClickListener(){
			
			@Override
			public void onClick(View v){
				Log.i("btncancel", "onclick");
				ad.cancel();
			}
		});
    }

	public void buildView(final View view){
		ViewGroup parent = (ViewGroup)root;
		parent.addView(view);
	}
	
	public void setBtnOkClickListener(android.view.View.OnClickListener l){
		btnOk.setOnClickListener(l);
		mOnOkClickListener = l;
	}
	
	public void cancel(){
		ad.cancel();
	}
	
	public void onOkClick(){
		if(mOnOkClickListener!=null){
			mOnOkClickListener.onClick(btnOk);
		}
	}
	
}
